package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 添加
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        log.info("[检查组-添加]data:{},checkitemIds:{}",checkGroup,checkitemIds);
        try {
            checkGroupService.add(checkGroup,checkitemIds);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.ADD_CHECKGROUP_FAIL+":"+e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */

    @GetMapping("findPage")
    public PageResult findPage(QueryPageBean queryPageBean){
        log.info("[检查组-分页查询]data:{}",queryPageBean);
        try {
            return checkGroupService.findPage(queryPageBean);
        } catch (RuntimeException e) {
            log.error("[检查组-分页查询]",e);
            return new PageResult(0L, Collections.emptyList());
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){
        log.info("[检查组-根据id查询]id:{}",id);
        try {
            CheckGroup checkGroup=checkGroupService.findById(id);
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL+":"+e.getMessage());

        }
    }

    /**
     * 根据检查组ID查询关联的检查项ID集和
     * @param id
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        log.info("[检查组-根据检查组ID查询关联的检查项ID集和]id:{}",id);
        try {
            List<Integer> items=checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,items);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.ACTION_FAIL+":"+e.getMessage());

        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        log.info("[检查组-添加]data:{},checkitemIds:{}",checkGroup,checkitemIds);
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.ADD_CHECKGROUP_FAIL+":"+e.getMessage());
        }
    }

    @GetMapping("findAll")
    public Result findAll(CheckGroup checkGroup){
        log.info("[检查组-查询所有]data:{}",checkGroup);
        try {
            List<CheckGroup> checkGroups=checkGroupService.findAll(checkGroup);
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroups);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL+":"+e.getMessage());
        }
    }

}
